﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Framework.Verifications;
using System.Text.RegularExpressions;

namespace Pixysoft.Weblications.Sango.Formular
{
    class PartyFormular
    {
        /** 商会页面最大数 */
        public static int default_pagesize_party = 20;

        /** 商会创建费用 */
        public static int default_cashvalue_createparty = 500;

        /** 最大商会等级 */
        public static int default_maxlogolevel = 10;

        /** 商会升级 提升最大人数 */
        public static int default_membercount_levelup = 5;

        // 每页人数大小
        public const int default_pagesize_member = 10;

        // 科技最大等级
        public const int default_max_missionlevel = 100;

        // 军团默认初始人数
        public const int default_membercount = 50;

        /// <summary>
        /// 军团科技对资源加成百分比
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public static double GetResourceAddProductivityFactor(long missionlevel)
        {
            return 0.1 + 0.01 * missionlevel;
        }

        /// <summary>
        /// 升级军徽消耗银币
        /// </summary>
        /// <param name="logoLevel"></param>
        /// <returns></returns>
        public static int LogoLevelupMemberIncrease(long nextLogoLevel)
        {
            if (nextLogoLevel <= 2)
                return 100;
            if (nextLogoLevel <= 3)
                return 500;
            if (nextLogoLevel <= 4)
                return 1000;
            if (nextLogoLevel <= 5)
                return 5000;
            if (nextLogoLevel <= 6)
                return 10000;
            if (nextLogoLevel <= 7)
                return 50000;
            if (nextLogoLevel <= 8)
                return 100000;
            if (nextLogoLevel <= 9)
                return 500000;

            return 1000000;

        }

        /// <summary>
        /// 升级科技 到达最大值，返回false
        /// </summary>
        /// <param name="defaultmission"></param>
        /// <param name="donatevalue"></param>
        /// <param name="maxlevel"></param>
        public static bool upgradeMission(Entities.Usr_Partymission mission, long increaseExp, long maxLevel)
        {
            if (increaseExp < 0)
                throw Exceptions.VerificationFailedException(mission, increaseExp, maxLevel);

            long currentLevel = mission.Missionlevel;

            long currentExp = mission.Expvalue;

            long upgradeExp = MissionLevelupValue(mission.Missiontypecode, currentLevel);

            do
            {
                // 如果当前值+增量比升级要求小，则表示不会跨级，跳出循环

                if (currentExp + increaseExp < upgradeExp)
                    break;

                // 否则跨级在计算

                currentLevel++;

                increaseExp -= (upgradeExp - currentExp);

                currentExp = 0;

                upgradeExp = MissionLevelupValue(mission.Missiontypecode, currentLevel);
            }
            while (true);

            mission.Expvalue = currentExp + increaseExp;

            mission.Upgradeexpvalue = upgradeExp;

            mission.Missionlevel = (currentLevel);

            if (mission.Missionlevel >= maxLevel)
            {
                mission.Missionlevel = (maxLevel);
                mission.Expvalue = (0);
                mission.Upgradeexpvalue = MissionLevelupValue(mission.Missiontypecode, mission.Missionlevel);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 任务升级消耗银币
        /// </summary>
        /// <param name="typeCode"></param>
        /// <param name="missionLevel"></param>
        /// <returns></returns>
        public static long MissionLevelupValue(long typeCode, long missionLevel)
        {
            Entities.Pty_Partymission mission = PtyPartymissionHelper.searchByCode(typeCode);

            long nextlevel = missionLevel + 1;

            long cashvalue = mission.Cashvalue + nextlevel * mission.Increasecashvalue;

            return cashvalue;
        }

        /// <summary>
        /// 是否有效的名字
        /// </summary>
        /// <param name="partyName"></param>
        /// <returns></returns>
        public static bool isValidPartyName(string name)
        {
            if (Regex.IsMatch(name, "～|·|！|＠|＃|￥|％|＾|＆|×|（|）|—|—|－|＋|＝|｛|【|｝|】|｜|＼|：|；|“|‘|《|，|》|。|？|、"))
                return false;

            string validValue = ContentReviewHelper.Replace(name, 10);

            if (!name.Equals(validValue, StringComparison.OrdinalIgnoreCase))
                return false;

            return SettingHelper.OnlyMatch(name, "[\u0391-\uFFE5\u4e00-\u9fa5a-zA-Z0-9_]{1,10}");
        }

        /// <summary>
        /// 升级军徽消耗金币
        /// </summary>
        /// <param name="logoLevel"></param>
        /// <returns></returns>
        public static long LogoLevelGoldValue(long logoLevel)
        {
            if (logoLevel <= 2)
                return 100;
            if (logoLevel <= 3)
                return 500;
            if (logoLevel <= 4)
                return 1000;
            if (logoLevel <= 5)
                return 5000;
            if (logoLevel <= 6)
                return 10000;
            if (logoLevel <= 7)
                return 50000;
            if (logoLevel <= 8)
                return 100000;
            if (logoLevel <= 9)
                return 500000;

            return 1000000;
        }

        /// <summary>
        /// 升级军衔需要的等级
        /// </summary>
        /// <param name="position"></param>
        /// <param name="totalmembers"></param>
        /// <returns></returns>
        public static long getUpgradeRequiredRanking(Entities.Pty_Partytitle position, long totalmembers)
        {
            if (position.Totalmembercountfactor < 0)
                return totalmembers;

            return (long)Math.Round(totalmembers * position.Totalmembercountfactor / 100f);
        }

        /// <summary>
        /// 玩家每日最大投资额度
        /// </summary>
        /// <param name="playerlevel"></param>
        /// <returns></returns>
        public static long getTodayTotalDonateCashvalue(long playerlevel)
        {
            //目前不控制每日投资额度

            return 15000;
        }

        /// <summary>
        /// 投资获取军团科技贡献
        /// </summary>
        /// <param name="donatevalue"></param>
        /// <returns></returns>
        public static long getDonateExperienceValue(long donatevalue)
        {
            return donatevalue;
        }

        /// <summary>
        /// 今日是否可以继续投资
        /// </summary>
        /// <param name="history"></param>
        /// <param name="expvalue"></param>
        /// <param name="playerlevel"></param>
        /// <returns></returns>
        public static bool canMissionDonate(Pixysoft.Weblications.Sango.Entities.Usr_Activity history, long expvalue, long playerlevel)
        {
            return history.Partymissiondonatevalue + expvalue <= getTodayTotalDonateCashvalue(playerlevel);
        }

        /// <summary>
        /// 判断玩家是否可以升级官职
        /// </summary>
        /// <param name="position"></param>
        /// <param name="totalmembers"></param>
        /// <param name="ranking"></param>
        /// <param name="contribution"></param>
        /// <returns></returns>
        public static bool canMemberUpgrade(Entities.Pty_Partytitle position, long totalmembers, long ranking, long contribution)
        {
            long requireContribution = getUpgradeRequiredContribution(position);

            long requireRanking = getUpgradeRequiredRanking(position, totalmembers);

            LoggerHelper.Debug("canMemberUpgrade contribute.require:{0} contribute:{1} ranking.require:{2} ranking:{3} title:{4} totalmember:{5}",//
                    requireContribution, contribution, requireRanking, ranking, position.Titletypecode, totalmembers);

            if (contribution < requireContribution)
            {
                return false;
            }

            if (ranking > requireRanking)
            {
                return false;
            }



            return true;
        }

        /// <summary>
        /// 获取升级到目标头衔需要的贡献度
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public static long getUpgradeRequiredContribution(Entities.Pty_Partytitle position)
        {
            return position.Contributionvalue;

        }

        /// <summary>
        /// 是否需要降级 
        /// </summary>
        /// <param name="totalmembers"></param>
        /// <param name="currentmembers"></param>
        /// <param name="titletypecode"></param>
        /// <returns></returns>
        public static bool needMemberDowngrade(int totalmembers, int currentmembers, long titletypecode)
        {
            // 获取百分比
            Entities.Pty_Partytitle currentPosition = PtyPartyTitleHelper.searchByCode(titletypecode);

            long maxmembers = getTitleTypeMembers(currentPosition, totalmembers);

            if (maxmembers <= currentmembers)
                return true;

            return false;
        }

        /// <summary>
        /// 获取当前玩家当前头衔等级内的总人数系数
        /// </summary>
        /// <param name="position"></param>
        /// <param name="totalmembers"></param>
        /// <returns></returns>
        protected static long getTitleTypeMembers(Entities.Pty_Partytitle position, long totalmembers)
        {
            return totalmembers * position.Membercountfactor;
        }
    }
}
